package org.redmaple.core.util;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.commons.lang.StringUtils;
import org.redmaple.core.retrieving.Sort;
import org.redmaple.core.retrieving.Sort.SortType;
import org.redmaple.core.support.PageHead;
import org.redmaple.core.support.PageRequest;
import org.redmaple.core.support.ReadmapleCoreConstant.RequestParamterInfo;

/**
 * ServletRequest辅助工具类
 * 
 * @author Xiangyun
 *
 */
public final class ServletRequestUtils {

	public static Number getNumberParamter(ServletRequest request, String name,
			Number defaultValue) {
		String value = request.getParameter(name);
		if (value == null)
			return defaultValue;
		try {
			return defaultValue.getClass().getConstructor(String.class)
					.newInstance(value);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defaultValue;
	}

	public static List<Number> getNumberParamters(ServletRequest request,
			String name, Class<Number> clazzNumber) {
		String value = request.getParameter(name);
		if (value == null)
			return null;
		try {
			String[] values = value
					.split(RequestParamterInfo.REQUEST_DEFAULT_DATA_SEPARATOR);
			List<Number> ret = new ArrayList<Number>();
			for (String v : values) {
				if (StringUtils.isNotBlank(v)) {
					ret.add(clazzNumber.getConstructor(String.class)
							.newInstance(v));
				}
			}
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String getStringParamter(ServletRequest request, String name) {
		String value = request.getParameter(name);
		if (!StringUtils.isBlank(value)) {
			return value;
		}
		return null;
	}

	public static List<String> getStringParamters(ServletRequest request,
			String name, String separator) {
		String[] values = request.getParameterValues(name);
		if (null == values) {
			return null;
		}
		if (values.length == 1) {
			return Arrays.asList(values[0].split(separator));
		} else {
			return Arrays.asList(values);
		}
	}

	public static List<String> getStringParamters(ServletRequest request,
			String name) {
		return getStringParamters(request, name,
				RequestParamterInfo.REQUEST_DEFAULT_DATA_SEPARATOR);
	}

	public static Date getDateParemter(ServletRequest request, String name,
			Date defaultValue) {
		String value = request.getParameter(name);
		if (!StringUtils.isBlank(value)) {
			return DateUtils.parseToSqlDate(value);
		}
		return defaultValue;
	}

	public static Date getDateParemter(ServletRequest request, String name) {
		return getDateParemter(request, name, null);
	}

	public static List<Date> getDateParemters(ServletRequest request,
			String name) {
		return getDateParemters(request, name,
				RequestParamterInfo.REQUEST_DEFAULT_DATA_SEPARATOR);
	}

	public static List<Date> getDateParemters(ServletRequest request,
			String name, String separator) {
		String[] values = request.getParameterValues(name);
		if (null == values) {
			return null;
		}
		List<Date> ret = new ArrayList<Date>();
		if (values.length == 1) {
			values = values[0].split(separator);
		}
		for (String v : values) {
			if (StringUtils.isNotBlank(v)) {
				ret.add(DateUtils.parseToSqlDate(v));
			}
		}
		return ret;
	}

	public static Time getTimeParemter(ServletRequest request, String name,
			Time defaultValue) {
		String value = request.getParameter(name);
		if (!StringUtils.isBlank(value)) {
			return DateUtils.parseToSqlTime(value);
		}
		return defaultValue;
	}

	public static Time getTimeParemter(ServletRequest request, String name) {
		return getTimeParemter(request, name, null);
	}

	public static List<Time> getTimeParemters(ServletRequest request,
			String name) {
		return getTimeParemters(request, name,
				RequestParamterInfo.REQUEST_DEFAULT_DATA_SEPARATOR);
	}

	public static List<Time> getTimeParemters(ServletRequest request,
			String name, String separator) {
		String[] values = request.getParameterValues(name);
		if (null == values) {
			return null;
		}
		List<Time> ret = new ArrayList<Time>();
		if (values.length == 1) {
			values = values[0].split(separator);
		}
		for (String v : values) {
			if (StringUtils.isNotBlank(v)) {
				ret.add(DateUtils.parseToSqlTime(v));
			}
		}
		return ret;
	}

	public static Timestamp getTimestampParemter(ServletRequest request,
			String name, Timestamp defaultValue) {
		String value = request.getParameter(name);
		if (!StringUtils.isBlank(value)) {
			return DateUtils.parseToSqlTimestamp(value);
		}
		return defaultValue;
	}

	public static Timestamp getTimestampParemter(ServletRequest request,
			String name) {
		return getTimestampParemter(request, name, null);
	}

	public static List<Timestamp> getTimestampParemters(ServletRequest request,
			String name, String separator) {
		String[] values = request.getParameterValues(name);
		if (null == values) {
			return null;
		}
		List<Timestamp> ret = new ArrayList<Timestamp>();
		if (values.length == 1) {
			values = values[0].split(separator);
		}
		for (String v : values) {
			if (StringUtils.isNotBlank(v)) {
				ret.add(DateUtils.parseToSqlTimestamp(v));
			}
		}
		return ret;
	}

	public static List<Timestamp> getTimestampParemters(ServletRequest request,
			String name) {
		return getTimestampParemters(request, name, null);
	}

	public static PageHead getPageInfo(ServletRequest request) {
		int page = (Integer) getNumberParamter(request,
				RequestParamterInfo.REQUEST_PAGENO_NAME, 1);
		int rows = (Integer) getNumberParamter(request,
				RequestParamterInfo.REQUEST_PAGESIZE_NAME,
				PageRequest.DEFAULT_PAGE_SIZE);
		return new PageRequest(page, rows, 0);
	}

	public static Map<String, Object> getPropertiesMap(ServletRequest request) {
		Map<String, Object> propertiesMap = new HashMap<String, Object>();
		return propertiesMap;
	}

	public static List<Sort> getSortList(ServletRequest request) {
		List<Sort> sortValue = new ArrayList<Sort>();
		String sort = request
				.getParameter(RequestParamterInfo.REQUEST_PAGESORTPROPERTY_NAME);
		String order = request
				.getParameter(RequestParamterInfo.REQUEST_PAGESORTDIRECT_NAME);
		if (StringUtils.isNotBlank(sort)) {
			String[] sortName = sort
					.split(RequestParamterInfo.REQUEST_DEFAULT_DATA_SEPARATOR);
			String[] orderType = order
					.split(RequestParamterInfo.REQUEST_DEFAULT_DATA_SEPARATOR);
			SortType sortType = null;
			for (int i = 0; i < sortName.length; i++) {
				sortType = null;
				if (orderType.length < i || StringUtils.isBlank(orderType[i])) {
					sortValue.add(new Sort(sortName[i], SortType.ASC));
				} else {
					sortType = SortType.valueOf(orderType[i]);
					if (sortType == null) {
						sortType = SortType.ASC;
					}
					sortValue.add(new Sort(sortName[i], sortType));
				}
			}
		}
		return sortValue;
	}

}
